package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.model.UserName;
import com.example.myapplication.Dao.UserDao;
import com.example.myapplication.Fragment.Home;
import com.example.myapplication.Fragment.Mine;

public class LoginActivity extends AppCompatActivity {
    private EditText usernameEdit;
    private EditText passwordEdit;
    private TextView enter_login;
    private Button login;
    private CheckBox remember_password;
    private MyDatabaseHelper dbHelper;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().hide();

        pref = PreferenceManager.getDefaultSharedPreferences(this);
        remember_password = (CheckBox) findViewById(R.id.remember_password);

        enter_login = (TextView)findViewById(R.id.enter_login);
        usernameEdit = (EditText)findViewById(R.id.username_input);
        passwordEdit = (EditText)findViewById(R.id.password_input);
        login = (Button)findViewById(R.id.but_login);
        dbHelper = new MyDatabaseHelper(this,"memo.db",null,1);
        //密码样式
        passwordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
        //点击注册跳转到注册页面
        enter_login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }

        });

        //记住密码
        boolean isRemember = pref.getBoolean("remember_password1",false);
        if(isRemember){
            //将账号密码设置到文本框中
            String username1 = pref.getString("username","");
            String password1 = pref.getString("password","");
            usernameEdit.setText(username1);
            passwordEdit.setText(password1);
            remember_password.setChecked(true);
        }
        //点击登录按钮
        login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String username = usernameEdit.getText().toString().trim();
                String password = passwordEdit.getText().toString().trim();
//                System.out.println(username);
//                System.out.println(password);

                dbHelper.getWritableDatabase();
                //如果账号是admin，密码是123456，就认为登录成功
//                if(username.equals("admin")&&password.equals("123456")){
//                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//                    startActivity(intent);
//                    finish();
//                }else{
//                    Toast.makeText(LoginActivity.this,"username or password is invalid",Toast.LENGTH_LONG).show();
//                }
                if(username.isEmpty()||password.isEmpty()){
                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                }
                else{
                    if (login(username,password)) {
                        System.out.println(username);
                        System.out.println(password);


                        editor = pref.edit();
                        if(remember_password.isChecked()){//检查复选框是否被选中
                            editor.putBoolean("remember_password1",true);
                            editor.putString("username",username);
                            editor.putString("password",password);
                        }else {
                            editor.clear();
                        }
                        editor.apply();

                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        UserName.name = username;
                        Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    //验证登录
    public boolean login(String username,String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from User where username=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[] {username, password});
        if (cursor.moveToFirst()) {
            cursor.close();
            return true;
        }
        return false;
    }
}